<template>
  <div>
    <el-form :model="userForm" :rules="rules" ref="userForm" class="demo-userForm login-container">
      <h3 class="title">电商后台管理系统登录</h3>
      <!-- 用户名 prop 表单验证的规则 -->
      <el-form-item prop="username">
        <el-input prefix-icon="iconfont icon-login_user" v-model="userForm.username"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input prefix-icon="iconfont icon-login_password" v-model="userForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox class="remember" checked>记住密码</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button @click="login" type="primary" :loading="loading" style="width:100%">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
export default {
  // 导出一个组件的vue实例
  data() {
    return {
      loading: false,
      // 将来登录的时候,提交给服务器

      userForm: {
        username: "admin",
        password: "123456"
      },
      //   验证表单项使用
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    login() {
      this.$refs.userForm.validate(valid => {
        //   valid代表正确
        this.loading = true;
        if (valid) {
          this.$axios
            .post("login", this.userForm)
            .then(res => {
              console.log(res.data);
              this.loading = false;
              if (res.data.meta.status != 200) {
                this.$message.error(res.data.meta.msg);
              } else {
                this.$message({
                  message: res.data.meta.msg,
                  type: "success"
                });

                // 存到本地
                // cnosole.log(res.data.data.token)
                localStorage.setItem('mytoken',res.data.data.token)

                this.$router.push({path:"/layout/users"})
               
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>


<style scoped>
@import "../../assets/iconfont/iconfont.css";

.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 100px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.remember {
  margin: 0px 0px 10px 0px;
}
</style>

